<html>
  <head>
    <meta charset="UTF-8" />
    <title>Vue实例</title>
  </head>
  <body>
    <div id="app">
      单价：{{unitprice}} 数量:<counter @qtychange="handle"></counter>
      总价：{{total}}
    </div>
    <script src="../lib/vue.js"></script>
    <script>
      Vue.component('counter', {
        data: function () {
          return {
            count: 0,
          }
        },
        template: `

        <div>
          <input type="button" value="-" @click="jian"/>
          <span>{{count}}</span>
          <input type="button" value="+" @click="jia"/>
        </div>
        `,
        methods: {
          jian: function () {
            this.count--
            this.$emit('qtychange', this.count)
          },
          jia: function () {
            this.count++
            this.$emit('qtychange', this.count)
          },
        },
      })

      new Vue({
        el: '#app',
        data: {
          unitprice: 10,
          qty: 0,
        },
        computed: {
          total: function () {
            return this.unitprice * this.qty
          },
        },
        methods: {
          handle: function (qty) {
            console.log('接收到数量变化的事件')
            this.qty = qty
          },
        },
      })
    </script>
  </body>
</html>
